/*
 * @Date: 2019-11-03 11:58:12
 * @LastEditTime: 2022-11-04 18:01:20
 * @Description: 核心配置文件
 * @FilePath: \vue-leng\vue.config.js
 */ 
const { defineConfig } = require('@vue/cli-service')
const path = require('path')
const { name } = require('./package');
const CopyPlugin = require('copy-webpack-plugin')
// const { VantResolver } = require('unplugin-vue-components/resolvers');
// const ComponentsPlugin = require('unplugin-vue-components/webpack');
console.log('process.env', process.env.NODE_ENV)
const isProd = process.env.NODE_ENV === 'production'
const outputFileName = `static/js/[name]${isProd ? '.[contenthash:8]' : ''}.js`
module.exports = defineConfig({
  lintOnSave: false,
  // publicPath: '/components/',
  runtimeCompiler: true,
  outputDir: 'dist',
  assetsDir: 'static',
  filenameHashing: true,
  productionSourceMap: false,
  devServer: {
    host: '127.0.0.1',
    historyApiFallback: {
      rewrites: [{ from: /./, to: '/index.html' }],
    },
    allowedHosts: 'all',
    open: true,
    hot: true,
    port: 8888,
    client: {
      overlay: {
        errors: true,
        warnings: false,
      },
    },
    liveReload: true,
    headers: {
      'Access-Control-Allow-Origin': '*'
    }
  },

  configureWebpack: {
    snapshot: {
      // 这里必须配置,否则app-wxm 修改后,不能动态生效, 而且这里路径不用../../否则不生效;
      // managedPaths: [path.resolve(__dirname, '../node_modules')],
      managedPaths: [__filename],
      immutablePaths: [],
      buildDependencies: {
        hash: true,
        timestamp: true,
      },
      module: {
        timestamp: true,
      },
      resolve: {
        timestamp: true,
      },
      resolveBuildDependencies: {
        hash: true,
        timestamp: true,
      },
    },
    cache: {
      type: "filesystem",
      // profile: true,
      // memoryCacheUnaffected: true,
      buildDependencies: {
        config: [__filename] // 当你 CLI 自动添加它时，你可以忽略它
      },
      // managedPaths: []
    },
    resolve: {
      alias: {
        '@': path.resolve('src'),
        // '@':'src',
        // 'assets': '@/assets',
        // 'common': '@/common',
        // 'components': '@/components',
        // 'config': '@/config',
        // 'mixins': '@/mixins',
        // 'mock': '@/mock',
        // 'network': '@/network',
        // 'views': '@/views'
      }
    },
    output: {
      chunkLoadTimeout: 60000,
      // chunkLoadTimeout: 6000,
      library: `${name}-[name]`,
      libraryTarget: 'umd',
      chunkLoadingGlobal: `webpackJsonp_${name}`,
      filename: outputFileName,
    },
    plugins: [
      // new webpack.ProvidePlugin({
      //   $: "jquery",
      //   jQuery: "jquery",
      //   "windows.jQuery": "jquery"
      // }),
      // ComponentsPlugin({
      //   resolvers: [VantResolver()],
      // }),
      new CopyPlugin({
        patterns: [
          {
            from: path.resolve('public'),
            toType: 'dir',
            globOptions: {
              ignore: ['.DS_Store', '**/index.html'],
            },
            noErrorOnMissing: true,
          },
        ],
      }),
    ],
    module: {
      rules: [
        {
          test: /\.m?js$/,
          use: {
            loader: "babel-loader",
            options: {
              presets: ['@babel/preset-env']
            }
          }
        },
        {
          include: /node_modules/,
          test: /\.mjs$/,
          type: 'javascript/auto'
        }
      ]
     }
  },
  // 匹配: node_modules/app-wxm/src
  // transpileDependencies: [/[/\\]node_modules[/\\](.+?)?app-wxm(.*)[/\\]src/],
  css: {
    loaderOptions: {
      sass: {implementation: require('sass')},
      // less: {
      //   // 若 less-loader 版本小于 6.0，请移除 lessOptions 这一级，直接配置选项。
      //   lessOptions: {
      //     modifyVars: {
      //       // 直接覆盖变量
      //       'toast-default-width': '100px',
      //       'toast-default-min-height': '100px',
      //       'toast-line-height': '12px',
      //       'toast-text-padding': '12px',
      //       'toast-default-padding': '10px',
      //       'toast-icon-size': '50px',
      //       'toast-border-radius': '8px'
      //     },
      //   },
      // },
    },
  }
})
